﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObject;
using BusinessLogic;
using Utility;
namespace WebTeamplate
{

    public partial class Admin_ChiTietDMXe : BasePage
    {
        #region Fields
        public string urlList = "DMXe.aspx?cx={0}";
        public string validateGroup = "subCate";
        #endregion

        #region ViewStates
        public string Command
        {

            get
            {
                object o = ViewState["Command"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["Command"] = value; }

        }

        public int ID
        {

            get
            {

                object o = ViewState["ID"];

                if (o != null) return (int)o;

                return int.MinValue;

                //a default

            }

            set { ViewState["ID"] = value; }

        }

        #endregion

        #region Init,Loads
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSave.ClientID);
            if (!Page.IsPostBack)
            {
                CommonFunction.InitStatusPublishForComboboxDetail(drlPublish);
                CommonFunction.LoadSubCate(drlChungXe, CommonFunction.DEFAULT_CHUNGLOAIXE_CODE, true);
                InitParamater();
                InitTextControl();
                LoadData();
                drlPublish.Focus();
            }
        }

        private void LoadData()
        {
            DMXe objData = DMXeUtil.Get(this.ID);
            if (objData != null)
            {
                if (objData.IsPublished)
                    drlPublish.SelectedValue = "1";
                else drlPublish.SelectedValue = "0";
                txtName.Text = objData.TenXe;
                drlChungXe.SelectedValue = objData.ChungXe.ToString();
            }
        }

        #endregion

        #region Methos

        private void InitParamater()
        {
            drlChungXe.SelectedValue = CommonFunction.GetValueFromQueryString(this.Page, "cx");
            ID = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "id"));
            this.Command = CommonFunction.GetValueFromQueryString(this.Page, "act");
            if (this.Command == string.Empty)
                this.Command = CommonFunction.DEFAULT_COMMAND_ADD;
        }

        private void InitTextControl()
        {

            btnSave.Text = Resources.Resource.LABEL_BUTTON_SAVE;
            btnCacel.Text = Resources.Resource.LABEL_BUTTON_CANCEL;
            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;
            refvName.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblName));
            refvName.ValidationGroup = validateGroup;
            refvPublish.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblPublish));
            refvPublish.ValidationGroup = validateGroup;
            refvChungXe.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblChungXe));
            refvChungXe.ValidationGroup = validateGroup;

            vsumCategory.ValidationGroup = validateGroup;
            btnSave.ValidationGroup = validateGroup;
        }
        private void Update(DMXe objData)
        {
            objData.MaXe = this.ID;
            if (!DMXeUtil.Update(objData))
                lblErrorMessage.Text = Resources.Resource.MESSAGE_UPDATE_ERROR;
            else
                Response.Redirect(string.Format(urlList, drlChungXe.SelectedValue));
        }
        private void Insert(DMXe objData)
        {

            if (!DMXeUtil.Insert(objData))
                lblErrorMessage.Text = Resources.Resource.MESSAGE_INSERT_ERROR;

            else
                Response.Redirect(string.Format(urlList,drlChungXe.SelectedValue));

        }



        #endregion

        #region Events, Handler
      
        protected void btnCacel_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format(urlList,drlChungXe.SelectedValue));
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.GetCurrentPageAndParamaterUrl());
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                DMXe objData = new DMXe();
                if (drlPublish.SelectedValue == "1")
                    objData.IsPublished = true;
                else objData.IsPublished = false;                
                objData.TenXe = txtName.Text;
                objData.ChungXe = ConvertValueObject.ParseInt(drlChungXe.SelectedValue.ToString());
                objData.CreatedBy = objData.LastUpdatedBy = this.userLoginName;
                objData.CreatedDate = objData.LastUpdatedDate = CommonFunction.Now;
                if (this.Command == CommonFunction.DEFAULT_COMMAND_ADD)
                    Insert(objData);
                else
                    Update(objData);

            }
            else
            {
                vsumCategory.ShowMessageBox = false;
                vsumCategory.ShowSummary = true;
            }
        }
        #endregion
    }
}
